#!/usr/bin/env bash
set -xe

if [ -z "$STD_TARGETS" ]; then
    # if none specified, always build for these two targets
    if [ `uname` == "Darwin" ]; then
       STD_TARGETS="x86_64-apple-darwin-postgres aarch64-apple-darwin-postgres"
    else
       STD_TARGETS="x86_64-postgres-linux-gnu aarch64-postgres-linux-gnu"
    fi
fi

# and depending on the platform we're building on, we need to set a linker flag for the other
# this'll get hairy when we support more platforms and we should port this script to Rust

if [ `uname` == "Darwin" ]; then
   if [ `uname -m` == "arm64" ]; then
      if [[ -z "$CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER" ]] && [[ -z "$CARGO_TARGET_AARCH64_APPLE_DARWIN_POSTGRES_LINKER" ]]; then
           export CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER=cc
           export CARGO_TARGET_AARCH64_APPLE_DARWIN_POSTGRES_LINKER=cc
       fi
   elif [ `uname -m` == "x86_64" ]; then
       if [[ -z "$CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER" ]] && [[ -z "$CARGO_TARGET_X86_64_APPLE_DARWIN_POSTGRES_LINKER" ]]; then
           export CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER=cc
           export CARGO_TARGET_X86_64_APPLE_DARWIN_POSTGRES_LINKER=cc
       fi

   else
      echo unsupported macos build platform: $(uname -m)
      exit 1
   fi

else
   if [ `uname -m` == "x86_64" ]; then
       if [[ -z "$CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER" ]] && [[ -z "$CARGO_TARGET_AARCH64_POSTGRES_LINUX_GNU_LINKER" ]]; then
           export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
           export CARGO_TARGET_AARCH64_POSTGRES_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
       fi
   elif [ `uname -m` == "aarch64" ]; then
       if [[ -z "$CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER" ]] && [[ -z "$CARGO_TARGET_X86_64_POSTGRES_LINUX_GNU_LINKER" ]]; then
           export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
           export CARGO_TARGET_X86_64_POSTGRES_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
       fi
   else
       echo unsupported build platform: $(uname -m)
       exit 1
   fi
fi

# Make sure the tip of pgrx's develop branch is used,
# until a release that has all the necessary features is cut.
cargo update -p pgrx
cargo fetch
if [ "$CI" != true ]; then
    # Attempt to get pgrx version from cargo tree. Looking for a pattern such as "pgrx v0.10.0"
    PGRX_VERSION=$(cargo tree --depth 1 --package plrust | grep -E "\s*pgrx\s+v[0-9]+\.[0-9]+\.[0-9]+" | head -n 1 | cut -f2- -dv)

    if [ -z "$PGRX_VERSION" ]; then
        echo "Could not determine pgrx version from 'cargo tree'!"
        exit 1
    else
        echo "Installing cargo-pgrx version $PGRX_VERSION"
        cargo install cargo-pgrx \
            --version "$PGRX_VERSION" \
            --locked # use the Cargo.lock in the pgrx repo
    fi
fi

# Don't need to run cargo pgrx init: user might already have set it up,
# and doing so risks clobbering their configuration.
# If they get an error, it's fairly self-explanatory.

(
    if cd postgrestd; then
        git pull
        git submodule update --init --recursive
    else
        git clone https://github.com/tcdi/postgrestd.git --branch "rust-1.72.0" --recurse-submodules
        cd ./postgrestd
    fi
    rm -f rust-toolchain.toml
    STD_TARGETS="$STD_TARGETS" ./run clean
    STD_TARGETS="$STD_TARGETS" ./run install
)
